# ----------------------------
# JungleGym Generator 4 blender
# V 1.0
# Dietmar Schreiner
# ----------------------------

import bpy
import mathutils
from bpy.props import FloatVectorProperty
from add_utils import AddObjectHelper, add_object_data
from mathutils import Vector


# ============================
# JungleGym Data:
# 24x24

jungleGym_ground = [
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,10,11],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,10,11],
    [0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,6,0,0,0,0,0,0,5,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
]


# ----------------------------

jungleGym_height = 10
jungleGym_cellSize = 1
jungleGym_pipe_sizeRatio = 0.2


jungleGym_depth = len(jungleGym_ground)
jungleGym_width = len(jungleGym_ground[0])

jungleGym_metal = bpy.data.materials.new('metal material')
jungleGym_metal.diffuse_color = 0.55, 0.55, 0.55

jungleGym_wood = bpy.data.materials.new('wood material')
jungleGym_wood.diffuse_color = 0.4, 0.3, 0.0
    
# ============================

# create floor and ceiling
bpy.ops.mesh.primitive_cube_add(location=(0,0,0))
currentObject = bpy.context.object
currentObject.name="JungleGym_Floor"
currentObject.scale=(jungleGym_width/2,jungleGym_depth/2,jungleGym_cellSize/2)
mesh = currentObject.data
mesh.materials.append(jungleGym_wood)

bpy.ops.object.duplicate_move()
currentObject = bpy.context.scene.objects.active
currentObject.location=(0,0,jungleGym_height)
currentObject.name = "JungleGym_Ceiling"


# create pipes: prototypal, slow ;)
for x in range(0,jungleGym_depth):
        for y in range(0,jungleGym_width):
                if jungleGym_ground[x][y] != 0:
                        pipeID=jungleGym_ground[x][y]
                        for x2 in range(0,jungleGym_depth):
                                for y2 in range(0,jungleGym_width):
                                        if jungleGym_ceiling[x2][y2] == pipeID:
                                                print("creating pipe:",pipeID,jungleGym_ceiling[x][y],x,y,x2,y2)
                                                # calculate coordinates
                                                floor_x = ( x + ( 0 - jungleGym_depth/2 ) ) * jungleGym_cellSize + jungleGym_cellSize/2
                                                floor_y = ( y + ( 0 - jungleGym_width/2 ) ) * jungleGym_cellSize + jungleGym_cellSize/2
                                                floor_z=0
                                                ceiling_x = ( x2 + ( 0 - jungleGym_depth/2 ) ) * jungleGym_cellSize + jungleGym_cellSize/2
                                                ceiling_y = ( y2 + ( 0 - jungleGym_width/2 ) ) * jungleGym_cellSize + jungleGym_cellSize/2
                                                ceiling_z = jungleGym_height
                                                pipePath = Vector((ceiling_x-floor_x,ceiling_y-floor_y,ceiling_z-floor_z))
                                                pipeCenter = Vector((ceiling_x+floor_x,ceiling_y+floor_y,ceiling_z+floor_z))
                                                
                                                #create pipe
                                                bpy.ops.mesh.primitive_cylinder_add(vertices=16, radius=jungleGym_cellSize*jungleGym_pipe_sizeRatio, depth=pipePath.length,cap_ends=True, view_align=False, 
                                                                                    enter_editmode=False, location=(pipeCenter*0.5),rotation=(0,0,0),
                                                                                    layers=(True, False, False, False, False, False, False, False, False, False, 
                                                                                            False, False, False, False, False, False, False, False, False, False))
                                                currentObject = bpy.context.object
                                                currentObject.name = "Pipe_" + str(pipeID)
                                                
                                                # do transforms
                                                up_axis = mathutils.Vector([0.0, 0.0, 1.0])
                                                angle = pipePath.angle(up_axis, 0)
                                                print(angle)
                                                axis = up_axis.cross(pipePath)
                                                euler = mathutils.Matrix.Rotation(angle, 4, axis).to_euler()
                                                currentObject.rotation_euler = euler                                                
   
                                                mesh = currentObject.data
                                                mesh.materials.append(jungleGym_metal)
            
	
